<?php

namespace app\api\controller;

use app\common\controller\BasicApi;
use app\common\model\UserModel;
use app\common\model\MessageModel;

class Message extends BasicApi
{
    protected $member;
    protected $openid;

    public function _initialize()
    {
        parent::_initialize();
        $this->openid = isset($this->header['token'])?$this->header['token']:'';
    }

    /**
     * 获取列表
     * @throws \think\Exception\DbException
     */
    public function getList()
    {
        $user_info = $this->getMember();
        $list = MessageModel::where('read_uid', $user_info['id'])->all();
        $this->success('',$list,200);
    }

    public function read()
    {
        $id = $this->request->post('id');
        $message = MessageModel::get($id);
        $message->read_state = 1;
        $message->read_at = date('Y-m-d H:i:s');
        $message->save();
        $this->success('操作成功', [],200);
    }

    /**
     * 获取会员信息
     */
    protected function getMember()
    {
        return UserModel::where(['openid'=>$this->openid])->find();
    }
}